/**
 * 
 */
package soa.security.web.action;

import org.apache.commons.lang3.StringUtils;

import soa.security.domain.Department;
import soa.security.domain.Organization;
import soa.security.web.support.action.LdapModelAction;

/**
 * @author Cre.Gu
 *
 */
public class DepartmentAction extends LdapModelAction<Department> {

	private static final long serialVersionUID = -7501812861819154492L;

	private String pid;
	private String positionIds;
	private String groupIds;
	private String roleIds;

	// 删除原来的
	private String removeOrig;
	private String departmentOrigId;

	private String dids;
	private String pids;

	public String grid() {
		if (StringUtils.isBlank(organizationId))
			return SUCCESS;

		if (StringUtils.isBlank(pid))
			updateResultContent(departmentMgrService.allDepartments(
					organizationId, buildContext().getParameters(), subTree));
		else
			updateResultContent(departmentMgrService.allSubDepartments(
					organizationId, pid, buildContext().getParameters()));

		return SUCCESS;
	}

	public String edit() {
		Department department = getModel();
		if (StringUtils.isBlank(department.getBid())) {
			department.setBid("DE"
					+ departmentMgrService.findMaxNumber(Department.class));
			department.setParentBid(pid);
		}
		Organization or = department.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);

		department.setOrganization(or);
		updateResultContent(department);

		this.pageResult.addData("parentName",
				getDepartment(organizationId, department.getParentBid())
						.getName());

		return SUCCESS;
	}

	public String doneSave() {
		departmentMgrService.addDepartment(organizationId, getModel());
		return SUCCESS;
	}

	public String doneUpdate() {
		departmentMgrService.updateDepartment(organizationId, getModel());
		return SUCCESS;
	}

	public String doneAddPersons() {
		departmentMgrService
				.addPersons(organizationId, departmentId, personIds);

		if ("TRUE".equals(removeOrig)) {
			if (StringUtils.isNotBlank(departmentOrigId))
				departmentMgrService.removePersons(organizationId,
						departmentOrigId, personIds);

			departmentMgrService.removePersonsOther(organizationId, dids, pids);
		}
		return SUCCESS;
	}

	public String doneRemovePersons() {
		departmentMgrService.removePersons(organizationId, departmentId,
				personIds);
		return SUCCESS;
	}

	public String doneAddPositions() {
		departmentMgrService.addPositions(organizationId, departmentId,
				positionIds);
		return SUCCESS;
	}

	public String doneRemovePositions() {
		departmentMgrService.removePositions(organizationId, departmentId,
				positionIds);
		return SUCCESS;
	}

	public String doneAddGroups() {
		departmentMgrService.addGroups(organizationId, departmentId, groupIds);
		return SUCCESS;
	}

	public String doneRemoveGroups() {
		departmentMgrService.removeGroups(organizationId, departmentId,
				groupIds);
		return SUCCESS;
	}

	public String doneAddRoles() {
		departmentMgrService.addRoles(organizationId, departmentId, roleIds);
		return SUCCESS;
	}

	public String doneRemoveRoles() {
		departmentMgrService.removeRoles(organizationId, departmentId, roleIds);
		return SUCCESS;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPositionIds() {
		return positionIds;
	}

	public void setPositionIds(String positionIds) {
		this.positionIds = positionIds;
	}

	public String getGroupIds() {
		return groupIds;
	}

	public void setGroupIds(String groupIds) {
		this.groupIds = groupIds;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public String getRemoveOrig() {
		return removeOrig;
	}

	public void setRemoveOrig(String removeOrig) {
		this.removeOrig = removeOrig;
	}

	public String getDepartmentOrigId() {
		return departmentOrigId;
	}

	public void setDepartmentOrigId(String departmentOrigId) {
		this.departmentOrigId = departmentOrigId;
	}

	public String getDids() {
		return dids;
	}

	public void setDids(String dids) {
		this.dids = dids;
	}

	public String getPids() {
		return pids;
	}

	public void setPids(String pids) {
		this.pids = pids;
	}

	@Override
	protected Department findByDn(String dn) {
		return departmentMgrService.findByDn(dn);
	}

}
